#include <iostream>
using namespace std;

// 转成2进制的方式
int  NumberOf1(int n)
{
    if(n == 0) return 0;
    long num = n;
    if(num < 0) // 负数，求补码
    {
        num = 0x100000000 + n;
    }

    int index = 0;
    int result = 0;
    while(num)
    {
        int y = num % 2;
        num = num / 2;
        if(y == 1) result++;
        index++;
        if(index >= 32) break;
    }
    return result;
}

int  NumberOf1_2(int n)
{
    int result = 0;
    int i = 0;
    do
    {
        int t = n & 0x1;
        if(t == 1) result++;
        n = n >> 1;
        i++;
    } while (n && i < 32);
    return result;
}

int main()
{
    // 普通测试
    cout << NumberOf1_2(1) << endl;
    cout << NumberOf1_2(7) << endl;
    cout << NumberOf1_2(-3) << endl;
    //边界测试
    cout << NumberOf1_2(0) << endl;
    cout << NumberOf1_2(-1) << endl;
    cout << NumberOf1_2(-2147483648) << endl;
    return 0;
}